package com.gjy.util.steam;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * 泛型表述:
 * Integer T: 流中元素的类型
 * Map<Boolean, List<Integer>> A: 累加器类型
 * Map<Boolean, List<Integer>> R: collect操作的结果类型
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-10-24 17:25:27
 */
public class PrimeNumberCollect implements Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> {

    @Override
    public Supplier<Map<Boolean, List<Integer>>> supplier() {
        return () -> new HashMap<Boolean, List<Integer>>() {{
            put(true, new ArrayList<>());
            put(false, new ArrayList<>());
        }};
    }

    @Override
    public BiConsumer<Map<Boolean, List<Integer>>, Integer> accumulator() {
        return (m, item) -> m.get(isPrime(item)).add(item);
    }

    @Override
    public BinaryOperator<Map<Boolean, List<Integer>>> combiner() {
        return (l, r) -> {
            l.get(true).addAll(r.get(true));
            l.get(false).addAll(r.get(false));
            return l;
        };
    }

    @Override
    public Function<Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
    }

    private Boolean isPrime(Integer i) {
        return i % 2 == 0;
    }

}
